﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class OperatorRegistration : System.Web.UI.UserControl
{
    BankDataDataContext db = new BankDataDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Roles.IsUserInRole("Administrator"))
        {
            Response.Redirect("~/Account/Login.aspx");
        }
        string operatorId = null;
        if (Session["id"] != null)
        {
            operatorId = Session["id"].ToString();
        }
        if (operatorId != null && ViewState["isEditing"] == null)
        {
            ResetPassword.Visible = true;
            ViewState["isEditing"] = true;
            CreateUserButton.Text = "Обновить данные оператора";
            var userName = from bankOperators in db.Operators
                           join users in db.aspnet_Users on bankOperators.UserId equals users.UserId
                           where bankOperators.OperatorId == Convert.ToInt32(operatorId)
                           select users.UserName;
            var userEmail = from bankOperators in db.Operators
                            join members in db.aspnet_Memberships on bankOperators.UserId equals members.UserId
                            where bankOperators.OperatorId == Convert.ToInt32(operatorId)
                            select members.Email;
            var operatorName = from bankOperators in db.Operators where bankOperators.OperatorId == Convert.ToInt32(operatorId) select bankOperators.Name;
            UserName.Text = userName.First();
            UserName.Enabled = false;
            OperatorName.Text = operatorName.First();
            Email.Text = userEmail.First();
            Email.Enabled = false;
            ConfirmPassword.Enabled = false;
            Password.Enabled = false;
            PasswordRequired.Enabled = false;
            ConfirmPasswordRequired.Enabled = false;
        }
    }

    protected void CreateUserButton_Click(object sender, EventArgs e)
    {
        if (ViewState["isEditing"] != null)
        {
            if ((bool)ViewState["isEditing"])
            {
                UpdateUser();
                Response.Redirect("~/Admin/Operators.aspx");
            }
        }
        else
        {
            if (RegisterNewUser())
            {
                Response.Redirect("~/Admin/Operators.aspx");
            }
        }

    }

    protected void UpdateUser()
    {
        string operatorId = null;
        if (Session["id"] != null)
        {
            operatorId = Session["id"].ToString();
        }
        var operators = from bankOperators in db.Operators where bankOperators.OperatorId == Convert.ToInt32(operatorId) select bankOperators;
        operators.First().Name = OperatorName.Text;
        db.SubmitChanges();
        if (Password.Enabled)
        {
            MembershipUser user = Membership.GetUser(UserName.Text);
            user.ChangePassword(user.GetPassword(), Password.Text);
        }
        Response.Redirect("~/Admin/Operators.aspx");
    }

    protected bool RegisterNewUser()
    {
        MembershipUser user;
        try
        {
            user = Membership.CreateUser(UserName.Text, Password.Text, Email.Text);
            Roles.AddUserToRole(user.UserName, "Operator");
        }
        catch
        {
            ErrorLabel.Text = "Логин занят";
            return false;
        }
        Operator newOperator = new Operator();
        newOperator.Name = OperatorName.Text;
        newOperator.UserId = (Guid)user.ProviderUserKey;
        db.Operators.InsertOnSubmit(newOperator);
        db.SubmitChanges();
        return true;
    }
    protected void ResetPassword_Click(object sender, EventArgs e)
    {
        ConfirmPassword.Enabled = true;
        Password.Enabled = true;
        PasswordRequired.Enabled = true;
        ConfirmPasswordRequired.Enabled = true;
    }
}